package com.sto.stoapp.activity.administrator;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.activity.scanner.StoRecordscheckActivity;
import com.sto.stoapp.adapter.SoleAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.http.BaseMessage;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.util.UnifiedStaticType;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

import sto.android.app.StoPdaKeyEvent;

public class AdminRecordQueryActivity extends StoHomeActivity {
	
	private ListView queryList;

    private SoleAdapter soleAdapter;
    
 // 按键映射
    private static final int[] MAPP_TO_KEY = { R.string.site_point_scan,
            R.string.site_point_sales, R.string.center_point, R.string.aviation_point };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_record_query);
        this.setTitleBottom(R.string.record_search,
                HomeBroadAction.ADMINISTRATOR_BROAD);
        initWidget();

    }

    private void initWidget() {
    	queryList = (ListView) findViewById(R.id.admin_query_list);
        soleAdapter = new SoleAdapter(this, getResourceList());
        queryList.setAdapter(soleAdapter);
        queryList.setOnItemClickListener(new StoOnItemClickListener(this) {

            @Override
            public void KcOnItemClickListener(AdapterView<?> parent, View view,
                    int position, long id) {
                view.setSelected(true);
                TextView title = (TextView) view.findViewById(R.id.res_test);
                clickItemEvent(title.getText().toString());

            }
        });

    }
    
    
    private void clickItemEvent(String title) {
        if (title.equals(getText(R.string.site_point_scan))) {
        	UnifiedStaticType.loginType = BaseMessage.SCANNER_VERSION;
        	Intent intent = new Intent(this, StoRecordscheckActivity.class);
            intent.putExtra("busiType", HomeBroadAction.ADMINISTRATOR_BROAD);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        // 网点-业务员
        else if (title.equals(getText(R.string.site_point_sales))) {
        	UnifiedStaticType.loginType = BaseMessage.SALES_VERSION;
        	Intent intent = new Intent(this, StoRecordscheckActivity.class);
            intent.putExtra("busiType", HomeBroadAction.ADMINISTRATOR_BROAD);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        // 中心
        else if (title.equals(getText(R.string.center_point))) {
        	UnifiedStaticType.loginType = BaseMessage.CENTER_VERSION;
        	Intent intent = new Intent(this, StoRecordscheckActivity.class);
			intent.putExtra("busiType", HomeBroadAction.ADMINISTRATOR_BROAD);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
        }
        // 航空
        else if (title.equals(getText(R.string.aviation_point))) {
        	UnifiedStaticType.loginType = BaseMessage.AVIATION_A_VERSION;
        	Intent intent = new Intent(this, StoRecordscheckActivity.class);
        	intent.putExtra("bus", HomeBroadAction.AVIATION_SCANNER_BROAD);
			intent.putExtra("busiType", HomeBroadAction.ADMINISTRATOR_BROAD);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
        }
    }
    
    private List<FunctionEntity> getResourceList() {
        List<String> resList = new ArrayList<String>();
        resList.add(getText(R.string.site_point_scan).toString());
        resList.add(getText(R.string.site_point_sales).toString());
        resList.add(getText(R.string.center_point).toString());
        resList.add(getText(R.string.aviation_point).toString());

        List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
        for (String res : resList) {
            FunctionEntity entity = new FunctionEntity();
            entity.setTitle(res);
            functionList.add(entity);
        }
        return functionList;
    }
    

    
    /**
     * 按键映射
     * 
     */
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if (keyCode > StoPdaKeyEvent.KEYCODE_0
	             && keyCode <= StoPdaKeyEvent.KEYCODE_0 + MAPP_TO_KEY.length) {
	         clickItemEvent(getString(MAPP_TO_KEY[keyCode - StoPdaKeyEvent.KEYCODE_1]));
	     }
	     return super.onKeyDown(keyCode, event);
	 }
}
